﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Linq;
using DatingSoonMVC2008.Models;
using System.Web.Security;
using System.Web.Routing;

namespace DatingSoonMVC2008.Controllers
{
    public class HomeController : Controller
    {
        private DatingSoonDBDataContext db = new DatingSoonDBDataContext();
        public IFacade Facade;
        
        public HomeController()
        { 
            Facade = new Facade(db);
        }
        public HomeController(IViewEngine viewEngine,IFacade facade)
        {
            this.ViewEngine = viewEngine;
            this.Facade = facade;
        }

        public void Index()
        {
            if (HttpContext.User.Identity.IsAuthenticated)
                RenderView("Authenticated");
            else
                RenderView("Index");
        }

        public void LoginSubmit(string name, string password)
        {
            if (Facade.CheckCredentials(name, password))
            {
                SetAuthCookie(name);
            }
            RedirectToAction("Index");
        }

        public virtual void SetAuthCookie(string name)
        {
            FormsAuthentication.SetAuthCookie(name, false);
        }
    }
}
